﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using com.vs.General;
using com.vs.DataAccess;

namespace com.vs.DataAccess.DO
{
    public partial class StorageTemplateDO : BaseDO
    {

        #region "Get Model"
        /// <summary>
        /// 将符合数据库主键的持久对象提取到模型对象中，如数据库符合条件的持久对象不存在，则返回空对象
        /// </summary>
        /// <remarks></remarks>
        public static StorageTemplateDO GetModelByPK(Session sess, string storageTempDO, int iSeq)
        {
            DataHelper helper = null;
            DataTable dtModel = null;
            string stSQL = "SP_STORAGETEMPLATE_SelectByPK";
            try
            {
                helper = new DataHelper(sess);
                dtModel = helper.GetTable(CommandType.StoredProcedure, stSQL, new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@STORAGE_TEMPLATE", storageTempDO),
                    DataHelper.BuildParameter(sess, "@SEQUENCE", iSeq)
                });
                if (!DataManager.IsEmpty(dtModel))
                {
                    return GetModel(dtModel.Rows[0]);
                }
                else
                {
                    return null;
                }
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }

        }

        #endregion

        #region "DataBase Operation"
        /// <summary>
        /// 在数据库里创建该数据访问对象的持久化数据，同时自增长属性OBJECT_ID也会同时得到更新,并提供数据库事务支持
        /// </summary>
        /// <remarks></remarks>
        public static void Insert(Session sess, StorageTemplateDO storageTempDO)
        {
            DataHelper helper = null;
            IDataParameter[] paras = null;
            string stSQL = "SP_STORAGETEMPLATE_Insert";
            try
            {
                helper = new DataHelper(sess);
                paras = ValueParas(storageTempDO);
                helper.Insert(CommandType.StoredProcedure, stSQL, paras);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }

        }

        public static void Delete(Session sess, StorageTemplateDO storageTempDO)
        {
            DataHelper helper = null;
            string stSQL = "SP_STORAGETEMPLATE_DeleteByPK";
            try
            {
                helper = new DataHelper(sess);
                helper.Update(CommandType.StoredProcedure, stSQL, UpdateWherePara(storageTempDO));
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }


        }

        /// <summary>
        /// 将该数据访问对象的变化同步到数据库中，如果该数据访问对象在数据库中的持久数据不存在，同步不会发生，同时也不会抛出例外
        /// </summary>
        /// <remarks></remarks>
        public static void Update(Session sess, StorageTemplateDO storageTempDO)
        {
            DataHelper helper = null;
            string stSQL = "SP_STORAGETEMPLATE_Update";
            try
            {
                helper = new DataHelper(sess);
                helper.Update(CommandType.StoredProcedure, stSQL, ValueParas(storageTempDO));
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }

        }

        #endregion

        #region "Data Load"
        /// <summary>
        /// 装载对象STORAGETEMPLATE的所有持久性对象到集合，如果数据量大，调用此方法会造成性能上的问题，请谨慎使用
        /// </summary>
        /// <remarks></remarks>
        public static List<StorageTemplateDO> GetAll(Session sess)
        {
            DataHelper helper = null;
            DataTable dtModel = null;
            string stSQL = "SP_STORAGETEMPLATE_SelectAll";
            try
            {
                helper = new DataHelper(sess);
                dtModel = helper.GetTable(CommandType.StoredProcedure, stSQL);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }


        }

        /// <summary>
        /// 装载指定列的值等于指定值的的所有持久性STORAGETEMPLATE对象到集合
        /// </summary>
        /// <remarks></remarks>
        public static List<StorageTemplateDO> GetListByColumn(Session sess, string columnName, Object value)
        {
            DataHelper helper = null;
            DataTable dtModel = null;
            IDataParameter[] paras = null;
            string stSQL = null;
            try
            {
                helper = new DataHelper(sess);
                stSQL = SelectSequel + " Where [" + columnName + "] =@Value ";
                paras = new IDataParameter[]{
                    new SqlParameter( "@Value", value)
                };
                dtModel = helper.GetTable(CommandType.Text, stSQL, paras);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }

        }

        /// <summary>
        /// 从DataView装载持久性STORAGETEMPLATE对象到集合
        /// </summary>
        /// <remarks></remarks>
        private static List<StorageTemplateDO> LoadListFromDataView(DataView dv)
        {
            List<StorageTemplateDO> list = new List<StorageTemplateDO>();
            for (int index = 0; index <= dv.Count - 1; index++)
            {
                list.Add(GetModel(dv[index].Row));
            }
            return list;
        }

        private List<StorageTemplateDO> LoadListFromDataReader(IDataReader dataReader)
        {
            List<StorageTemplateDO> list = new List<StorageTemplateDO>();
            while (dataReader.Read())
            {
                list.Add(GetModel(dataReader));
            }
            return list;
        }

        public static List<StorageTemplateDO> QuickSearch(Session sess, string searchKey)
        {
            DataHelper helper = null;
            DataTable dt = null;

            try
            {
                helper = new DataHelper(sess);
                dt = helper.GetTable(CommandType.StoredProcedure,
                    "SP_LOCATINGRULEHEADER_QuickSearch",
                    new IDataParameter[] { DataHelper.BuildParameter(sess, "@searchKey", "%" + searchKey.ToUpper().Trim() + "%") });
                return LoadListFromDataView(dt.DefaultView);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }
        /// <summary>
        /// loading data value  to property from Datarow
        /// </summary>
        /// <remarks></remarks>
        private static StorageTemplateDO GetModel(DataRow row)
        {
            StorageTemplateDO storageTempDO = new StorageTemplateDO();
            if (!row["STORAGE_TEMPLATE"].Equals(DBNull.Value)) storageTempDO.STORAGE_TEMPLATE = Convert.ToString(row["STORAGE_TEMPLATE"]);
            if (!row["SEQUENCE"].Equals(DBNull.Value)) storageTempDO.SEQUENCE = Convert.ToInt32(row["SEQUENCE"]);
            if (!row["UNIT_OF_MEASURE"].Equals(DBNull.Value)) storageTempDO.UNIT_OF_MEASURE = Convert.ToString(row["UNIT_OF_MEASURE"]);
            if (!row["USER_DEF1"].Equals(DBNull.Value)) storageTempDO.USER_DEF1 = Convert.ToString(row["USER_DEF1"]);
            if (!row["USER_DEF2"].Equals(DBNull.Value)) storageTempDO.USER_DEF2 = Convert.ToString(row["USER_DEF2"]);
            if (!row["USER_DEF3"].Equals(DBNull.Value)) storageTempDO.USER_DEF3 = Convert.ToString(row["USER_DEF3"]);
            if (!row["USER_DEF4"].Equals(DBNull.Value)) storageTempDO.USER_DEF4 = Convert.ToString(row["USER_DEF4"]);
            if (!row["USER_DEF5"].Equals(DBNull.Value)) storageTempDO.USER_DEF5 = Convert.ToString(row["USER_DEF5"]);
            if (!row["USER_DEF6"].Equals(DBNull.Value)) storageTempDO.USER_DEF6 = Convert.ToString(row["USER_DEF6"]);
            if (!row["USER_DEF7"].Equals(DBNull.Value)) storageTempDO.USER_DEF7 = Convert.ToDouble(row["USER_DEF7"]);
            if (!row["USER_DEF8"].Equals(DBNull.Value)) storageTempDO.USER_DEF8 = Convert.ToDouble(row["USER_DEF8"]);
            if (!row["USER_STAMP"].Equals(DBNull.Value)) storageTempDO.USER_STAMP = Convert.ToString(row["USER_STAMP"]);
            if (!row["PROCESS_STAMP"].Equals(DBNull.Value)) storageTempDO.PROCESS_STAMP = Convert.ToString(row["PROCESS_STAMP"]);
            if (!row["DATE_TIME_STAMP"].Equals(DBNull.Value)) storageTempDO.DATE_TIME_STAMP = Convert.ToDateTime(row["DATE_TIME_STAMP"]);
            if (!row["TREAT_FULL_PCT"].Equals(DBNull.Value)) storageTempDO.TREAT_FULL_PCT = Convert.ToDouble(row["TREAT_FULL_PCT"]);
            if (!row["GROUP_DURING_CHECKIN"].Equals(DBNull.Value)) storageTempDO.GROUP_DURING_CHECKIN = Convert.ToString(row["GROUP_DURING_CHECKIN"]);
            if (!row["OBJECT_ID"].Equals(DBNull.Value)) storageTempDO.OBJECT_ID = Convert.ToInt32(row["OBJECT_ID"]);
            return storageTempDO;
        }

        /// <summary>
        /// loading data value  to property from DataReader
        /// </summary>
        /// <remarks></remarks>
        private static StorageTemplateDO GetModel(IDataReader dataReader)
        {
            StorageTemplateDO storageTempDO = new StorageTemplateDO();
            if (!dataReader["STORAGE_TEMPLATE"].Equals(DBNull.Value)) storageTempDO.STORAGE_TEMPLATE = Convert.ToString(dataReader["STORAGE_TEMPLATE"]);
            if (!dataReader["SEQUENCE"].Equals(DBNull.Value)) storageTempDO.SEQUENCE = Convert.ToInt32(dataReader["SEQUENCE"]);
            if (!dataReader["UNIT_OF_MEASURE"].Equals(DBNull.Value)) storageTempDO.UNIT_OF_MEASURE = Convert.ToString(dataReader["UNIT_OF_MEASURE"]);
            if (!dataReader["USER_DEF1"].Equals(DBNull.Value)) storageTempDO.USER_DEF1 = Convert.ToString(dataReader["USER_DEF1"]);
            if (!dataReader["USER_DEF2"].Equals(DBNull.Value)) storageTempDO.USER_DEF2 = Convert.ToString(dataReader["USER_DEF2"]);
            if (!dataReader["USER_DEF3"].Equals(DBNull.Value)) storageTempDO.USER_DEF3 = Convert.ToString(dataReader["USER_DEF3"]);
            if (!dataReader["USER_DEF4"].Equals(DBNull.Value)) storageTempDO.USER_DEF4 = Convert.ToString(dataReader["USER_DEF4"]);
            if (!dataReader["USER_DEF5"].Equals(DBNull.Value)) storageTempDO.USER_DEF5 = Convert.ToString(dataReader["USER_DEF5"]);
            if (!dataReader["USER_DEF6"].Equals(DBNull.Value)) storageTempDO.USER_DEF6 = Convert.ToString(dataReader["USER_DEF6"]);
            if (!dataReader["USER_DEF7"].Equals(DBNull.Value)) storageTempDO.USER_DEF7 = Convert.ToDouble(dataReader["USER_DEF7"]);
            if (!dataReader["USER_DEF8"].Equals(DBNull.Value)) storageTempDO.USER_DEF8 = Convert.ToDouble(dataReader["USER_DEF8"]);
            if (!dataReader["USER_STAMP"].Equals(DBNull.Value)) storageTempDO.USER_STAMP = Convert.ToString(dataReader["USER_STAMP"]);
            if (!dataReader["PROCESS_STAMP"].Equals(DBNull.Value)) storageTempDO.PROCESS_STAMP = Convert.ToString(dataReader["PROCESS_STAMP"]);
            if (!dataReader["DATE_TIME_STAMP"].Equals(DBNull.Value)) storageTempDO.DATE_TIME_STAMP = Convert.ToDateTime(dataReader["DATE_TIME_STAMP"]);
            if (!dataReader["TREAT_FULL_PCT"].Equals(DBNull.Value)) storageTempDO.TREAT_FULL_PCT = Convert.ToDouble(dataReader["TREAT_FULL_PCT"]);
            if (!dataReader["GROUP_DURING_CHECKIN"].Equals(DBNull.Value)) storageTempDO.GROUP_DURING_CHECKIN = Convert.ToString(dataReader["GROUP_DURING_CHECKIN"]);
            if (!dataReader["OBJECT_ID"].Equals(DBNull.Value)) storageTempDO.OBJECT_ID = Convert.ToInt32(dataReader["OBJECT_ID"]);

            return storageTempDO;
        }

        #endregion

        #region "Other function"
        /// <summary>
        /// 该数据访问对象从数据库中提取数据的Sql语句 
        /// </summary>
        /// <remarks></remarks>
        private static string SelectSequel
        {
            get
            {
                return "Select	*  From [STORAGE_TEMPLATE] ";
            }
        }

        /// <summary>
        /// 该数据访问对象的属性值装载到数据库更新参数数组
        /// </summary>
        /// <remarks></remarks>
        private static IDbDataParameter[] ValueParas(StorageTemplateDO storageTempDO)
        {
            SqlParameter[] paras = new SqlParameter[17];
            paras[0] = new SqlParameter("@STORAGE_TEMPLATE", storageTempDO.STORAGE_TEMPLATE);
            paras[1] = new SqlParameter("@SEQUENCE", storageTempDO.SEQUENCE);
            paras[2] = new SqlParameter("@UNIT_OF_MEASURE", storageTempDO.UNIT_OF_MEASURE == String.Empty ? DBNull.Value : (object)storageTempDO.UNIT_OF_MEASURE);
            paras[3] = new SqlParameter("@USER_DEF1", storageTempDO.USER_DEF1 == String.Empty ? DBNull.Value : (object)storageTempDO.USER_DEF1);
            paras[4] = new SqlParameter("@USER_DEF2", storageTempDO.USER_DEF2 == String.Empty ? DBNull.Value : (object)storageTempDO.USER_DEF2);
            paras[5] = new SqlParameter("@USER_DEF3", storageTempDO.USER_DEF3 == String.Empty ? DBNull.Value : (object)storageTempDO.USER_DEF3);
            paras[6] = new SqlParameter("@USER_DEF4", storageTempDO.USER_DEF4 == String.Empty ? DBNull.Value : (object)storageTempDO.USER_DEF4);
            paras[7] = new SqlParameter("@USER_DEF5", storageTempDO.USER_DEF5 == String.Empty ? DBNull.Value : (object)storageTempDO.USER_DEF5);
            paras[8] = new SqlParameter("@USER_DEF6", storageTempDO.USER_DEF6 == String.Empty ? DBNull.Value : (object)storageTempDO.USER_DEF6);
            paras[9] = new SqlParameter("@USER_DEF7", storageTempDO.USER_DEF7);
            paras[10] = new SqlParameter("@USER_DEF8", storageTempDO.USER_DEF8);
            paras[11] = new SqlParameter("@USER_STAMP", storageTempDO.USER_STAMP == String.Empty ? DBNull.Value : (object)storageTempDO.USER_STAMP);
            paras[12] = new SqlParameter("@PROCESS_STAMP", storageTempDO.PROCESS_STAMP == String.Empty ? DBNull.Value : (object)storageTempDO.PROCESS_STAMP);
            paras[13] = new SqlParameter("@DATE_TIME_STAMP", System.DateTime.Now);
            paras[14] = new SqlParameter("@TREAT_FULL_PCT", storageTempDO.TREAT_FULL_PCT);
            paras[15] = new SqlParameter("@GROUP_DURING_UNITIZATION", storageTempDO.GROUP_DURING_CHECKIN == String.Empty ? DBNull.Value : (object)storageTempDO.GROUP_DURING_CHECKIN);
            paras[16] = new SqlParameter("@OBJECT_ID", storageTempDO.OBJECT_ID);

            paras[0].DbType = DbType.String;
            paras[1].DbType = DbType.Int32;
            paras[2].DbType = DbType.String;
            paras[3].DbType = DbType.String;
            paras[4].DbType = DbType.String;
            paras[5].DbType = DbType.String;
            paras[6].DbType = DbType.String;
            paras[7].DbType = DbType.String;
            paras[8].DbType = DbType.String;
            paras[9].DbType = DbType.Double;
            paras[10].DbType = DbType.Double;
            paras[11].DbType = DbType.String;
            paras[12].DbType = DbType.String;
            paras[13].DbType = DbType.DateTime;
            paras[14].DbType = DbType.Decimal;
            paras[15].DbType = DbType.String;
            paras[16].DbType = DbType.Int32;

            return paras;
        }

        private static IDbDataParameter[] UpdateWherePara(StorageTemplateDO storageTempDO)
        {
            SqlParameter[] paras = new SqlParameter[2];
            paras[0] = new SqlParameter("@STORAGE_TEMPLATE", storageTempDO.STORAGE_TEMPLATE);
            paras[1] = new SqlParameter("@SEQUENCE", storageTempDO.SEQUENCE);
            return paras;
        }

        #endregion

    }
}
